//
//  ViewController.m
//  03-UIGestureRecognizer
//
//  Created by 韩旭 on 16/7/18.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UIView *viewHand;
@property (nonatomic, strong) UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //  手势识别器  UIGestureRecognizer
//    UIGestureRecognizer 作为所有手势的基类（父类）
    //    分为七种
    //   1. 轻拍
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    //    点击几次触发
    tap.numberOfTapsRequired = 1;
    //    几个手指触发(按option - alt) 默认是一个
    tap.numberOfTouchesRequired = 1;
//    [self.view addGestureRecognizer:tap];
    
    //   2. 长按
    UILongPressGestureRecognizer *longtouch = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
//    长按几秒后开始, 默认是0.5s
    longtouch.minimumPressDuration = 1;
//    默认是10像素，长按失败前, 最大的移动距离
    longtouch.allowableMovement = 10;
//    [self.view addGestureRecognizer:longtouch];
    //   3. 清扫
//    
    UISwipeGestureRecognizer *swith = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swithWithBtn:)];
//      direction 可以设置多个, 前提多个方向触发的行为相同
//    如果多个枚举方向触发的行为不同, 需要创建多个清扫的手势
//   左右与上下有冲突
//  练习：ImageView添加清扫换图
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 355, 647)];
    self.imageView.backgroundColor = [UIColor cyanColor];
    self.imageView.image = [UIImage imageNamed:@"020035625"];
    self.imageView.userInteractionEnabled = YES;
    [self.view addSubview:_imageView];
    swith.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp;
//    [_imageView addGestureRecognizer:swith];
//    按位枚举, 4个选项 -> 1 1 1 1
//    全选的情况 1（4）1（3）1（2）1（1）1（0）
    
    
    
    
//    swith.direction = UISwipeGestureRecognizerDirectionRight;
//    [_imageView addGestureRecognizer:swith];
    
    
//     UISwipeGestureRecognizer *swith1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swithWithBtnS:)];
//    swith.direction = UISwipeGestureRecognizerDirectionLeft;
//    [_imageView addGestureRecognizer:swith1];

    
    //   4. 平移
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [self.view addGestureRecognizer:pan];
//    捏合手势
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [_imageView
     addGestureRecognizer:pinch];
    //    旋转
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotion:)];
    [_imageView addGestureRecognizer:rotation];
    //    屏幕边缘手势
    
    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] init];
    screenEdgePan.edges = UIRectEdgeLeft;
    [screenEdgePan addTarget:self action:@selector(screen:)];
    [self.view addGestureRecognizer:screenEdgePan];
}
//轻拍的方法
- (void)tapAction:(UITapGestureRecognizer *)tap {
    NSLog(@"轻拍");
}
//长按手势方法
- (void)longPressAction:(UILongPressGestureRecognizer *)tap {
    if (tap.state == UIGestureRecognizerStateBegan) {
        NSLog(@"长按开始");
    } else if (tap.state == UIGestureRecognizerStateEnded) {
        NSLog(@"长按结束");
    }
}
- (void)swithWithBtn:(UISwipeGestureRecognizer *)swith {
    
//    UIImageView *unge = (UIImageView *)swith.view;
    NSLog(@"%ld", swith.direction);
       if (swith.direction & UISwipeGestureRecognizerDirectionRight) {
           
           
           NSLog(@"%ld", swith.direction);
//    unge.image = [UIImage imageNamed:@"01N8HEIAR80X"];
//           self.imageView = unge;
//       } else {
//    unge.image = [UIImage imageNamed:@"20150329_225211_791"];
//        self.imageView = unge;
       }
}
//旋转方法
- (void)rotion:(UIRotationGestureRecognizer *)rotion {
    NSLog(@"%@",rotion);
//    转换中, 根据转化继续改变
    rotion.view.transform = CGAffineTransformRotate(rotion.view.transform, rotion.rotation);
//    旋转的弧度制为0
    rotion.rotation = 0;
}

- (void)panAction:(UIPanGestureRecognizer *)pan {
//    平移开始
    if (pan.state == UIGestureRecognizerStateBegan) {
//       平移中

    } else if (pan.state == UIGestureRecognizerStateChanged) {
        //    平移手势根据其他View上移动的过程, 得到最终点
        CGPoint point = [pan translationInView:pan.view];
        //    根据Point使pan.view移动
        
        //    NSLog(@"%@", NSStringFromCGPoint(point));
        pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y);
        //    pan移动的点置空
        [pan setTranslation:CGPointZero inView:pan.view];
       
    } else {
//        平移结束
        
         pan.view.transform = CGAffineTransformIdentity;
    }
//    CGPoint movex = CGPointMake(point.x - pan.view.center.x, point.y - pan.view.center.y);
//    [pan setTranslation:CGPointMake(point.x, point.y) inView:pan.view];
    
}

- (void)screen:(UIScreenEdgePanGestureRecognizer *)screen {
    NSLog(@"屏幕边缘手势");
}
- (void)pinchAction:(UIPinchGestureRecognizer *)pin {
    NSLog(@"%f", pin.scale);
    pin.view.transform = CGAffineTransformScale(pin.view.transform, pin.scale, pin.scale);
//    设置缩放倍数1
    pin.scale = 1;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
